package com.jiandaoshou;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jiandaoshou.base.BaseActivity;
import com.jiandaoshou.entry.RefreshMoneyEvent;
import com.jiandaoshou.http.APIDefine;
import com.jiandaoshou.http.Http;
import com.jiandaoshou.utils.RxBus;
import com.jiandaoshou.utils.ToastUtil;
import com.jiandaoshou.utils.UserUtils;

import org.simple.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

public class TiXianAct extends BaseActivity implements View.OnClickListener,TextWatcher {
    //获取总金额
    private String count;
    private Button Btn_Submit;
    private TextView Tv_YinHangName;
    private TextView Tv_YuE,tv_allti;
    private EditText Edit_JinE;
    private String  Str_BankName;
    private String  Str_BankID;
    private float   TiQu_JinE;
    private String  Str_ShopId;
    private String time;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch(msg.what){
                case 0:
                    EventBus.getDefault().post("refresh","onRefresh");
                    RxBus.getDefault().post(new RefreshMoneyEvent(true));
                    TiXianAct.this.finish();
                    break;
             }
        }
    };
    @Override
    public boolean showBackLogo() {
        return true;
    }

    @Override
    protected int getContentViewRes() {
        return R.layout.activity_tixian;
    }

    @Override
    protected String getTitleText() {
        return "提现";
    }

    @Override
    public void init() {
        //获取shopId
        tv_allti = $(R.id.tv_allti);
        tv_allti.setOnClickListener(this);
        Str_ShopId=getIntent().getStringExtra("shopId");
        Btn_Submit = $(R.id.btn_submit);
        Tv_YuE = $(R.id.tv_money);
        Tv_YinHangName = $(R.id.tv_yinhang);
        Edit_JinE = $(R.id.edit_count);
        Edit_JinE.addTextChangedListener(this);
        //显示资总余额
        Tv_YuE.setText("零钱余额（" + UserUtils.getBalance(context) + ")");
        time=sdf.format(new Date());
        //添加监听
        Btn_Submit.setOnClickListener(this);
        Tv_YinHangName.setOnClickListener(this);


//        $(R.id.tvAddBankCard).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_submit:
                String Str_JinE="0.0";
                Str_JinE = Edit_JinE.getText().toString();
                if(TextUtils.isEmpty(Str_JinE)){
                    ToastUtil.toast("请输入提现金额");
                    return;
                }
                try{
                    TiQu_JinE=Float.valueOf(Str_JinE);
                }catch (Exception e){
                    ToastUtil.toast("金额输入错误");
                    return;
                }
                if(TiQu_JinE > UserUtils.getBalance(context)){
                    ToastUtil.toast("提现失败");
                    return;
                }
                if(TextUtils.isEmpty(Str_BankID)){
                    ToastUtil.toast("请选择银行卡");
                    return;
                }
                HashMap<String, String> map = new HashMap<>();
                //商户号
                map.put("strShopID",Str_ShopId);
                map.put("strGetMoneyCardID",Str_BankID);
                map.put("strApplyValue",TiQu_JinE+"");
                map.put("dtApplyTime",time);
                map.put("strRemark","");
                GetData(map);
                break;
            case R.id.tv_yinhang:
               Intent intent= new Intent(TiXianAct.this, MyYinHangList.class);
                intent.putExtra("shopId",Str_ShopId);
                startActivityForResult(intent, 0);
                break;
            case R.id.tv_allti:
                Edit_JinE.setText(UserUtils.getBalance(context)+"");
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==0) {
            switch(resultCode){
                case 1:
                    Str_BankName=data.getStringExtra("bankname");
                    Str_BankID=data.getStringExtra("bank_id");
                    String Str_CardNum=data.getStringExtra("cardnum");
                    Tv_YinHangName.setText(Str_BankName+"("+Str_CardNum.substring(Str_CardNum.length()-4,Str_CardNum.length())+")");
                    break;
            }
        }

    }

    private void GetData(HashMap<String,String> map) {
        Http.post(APIDefine.TF_Interface_ApplyMoney, map, new Http.HttpCallBack() {
            @Override
            public void onHttpSuccess(String response) throws Exception {
                Toast.makeText(TiXianAct.this,"提交成功!",Toast.LENGTH_LONG).show();
                handler.sendEmptyMessage(0);
            }
        });
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        String text = s.toString();
        if(TextUtils.isEmpty(text))return;
        float a = 0;
        try{
            a = Float.parseFloat(text);
        }catch (Exception e){
            return;
        }
        if(a > UserUtils.getBalance(context)){
            ToastUtil.toast("余额不足");
            Edit_JinE.setText(UserUtils.getBalance(context)+"");
            return;
        }

    }
}
